package nccu.model;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.compass.annotations.Searchable;
import org.compass.annotations.SearchableId;
import org.compass.annotations.SearchableProperty;

import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * This class represents the basic "creditunion" object that allows for bank
 * persistence and management.
 *
 * @author <a href="mailto:chris.oberle@gmail.com">Chris Oberle</a>
 */
@Entity
@Table(name = "credit_union")
@Searchable
@XmlRootElement
public class CreditUnion extends BaseObject implements Serializable {
     private static final long serialVersionUID = 3832626162173359412L;

    private Long id;

    @XStreamAlias("CreditUnionName")
    private String name; // required
    private Date charterDate; // required
    private String status; // required
    @XStreamAlias("CharterNumber")
    private Integer insurance; // required
    private String address; // required
    private String postalCode; // required
    private String city; // required
    private String state; // required
    @Transient
    @XStreamAlias("CreditUnionType")
    private String type; // transient
    private List<SubmissionData> submissions;



    /**
     * Default constructor - creates a new instance with no values set.
     */
    public CreditUnion(){}

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @SearchableId
    @Column (name="creditunion_id")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(nullable = false, length = 200, unique = true)
    @SearchableProperty
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "charter_date", length = 100, nullable = false, unique = false)
    @SearchableProperty
    public Date getCharterDate() {
        return charterDate;
    }

    public void setCharterDate(Date charterDate) {
        this.charterDate = charterDate;
    }

    @Column(name = "status", nullable = false, unique = false)
    @SearchableProperty
    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @Column(name = "insurance", nullable = false, unique = true)
    @SearchableProperty
    public Integer getInsurance() {
        return insurance;
    }

    public void setInsurance(Integer insurance) {
        this.insurance = insurance;
    }

    @Column(name = "address", nullable = false, unique = true)
    @SearchableProperty
    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Column(name = "postal_code", nullable = false, unique = false)
    @SearchableProperty
    public String getPostalCode() {
        return postalCode;
    }

    public void setPostalCode(String postalCode) {
        this.postalCode = postalCode;
    }

    @Column(name = "city", nullable = false, unique = false)
    @SearchableProperty
    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Column(name = "state", nullable = false, unique = false)
    @SearchableProperty
    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    @OneToMany ( cascade = { CascadeType.PERSIST, CascadeType.REMOVE })
    @JoinColumn ( name="creditunion_id")
    public List<SubmissionData> getSubmissions() {
        return submissions;
    }

    public void setSubmissions(List<SubmissionData> submissions) {
        this.submissions = submissions;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof CreditUnion)) return false;

        CreditUnion that = (CreditUnion) o;

        if (!insurance.equals(that.insurance)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return insurance.hashCode();
    }

    /**
     * {@inheritDoc}
     */
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE)
                .append(this.id)
                .append(this.insurance)
                .append(this.name)
                .append(this.address)
                .append(this.city)
                .append(this.state)
                .append(this.postalCode)
                .append(this.charterDate)
                .append(this.status)
                .toString();
    }
}
